﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBLL;


namespace BLL
{
    public class ChargeBLL:IChargeBLL
    {

        public Model.Accounts checkAccount(int userId)
        {
            Model.Accounts account = new Model.Accounts();
            DAL.AccountsDAL accountDal = (DAL.AccountsDAL)DALFAC.DataAccess.CreateAccounts();

            if (null != accountDal) 
            {
                if (accountDal.userExists(userId)) 
                {
                    account = accountDal.GetModelByUserId(userId);
                }
            }
            return account;
        }

        public bool onlineCharge()
        {
            throw new NotImplementedException();
        }

        public bool cardCharge(int accountId, string cardnumber)
        {
            bool validator = false;
            Model.Accounts account = new Model.Accounts();
            DAL.AccountsDAL accountDal = (DAL.AccountsDAL)DALFAC.DataAccess.CreateAccounts();

            if (null != accountDal)
            {
                if (accountDal.accountExists(accountId))
                {
                    account = accountDal.GetModel(accountId);
                }

                DAL.CardsDAL cardDal = (DAL.CardsDAL)DALFAC.DataAccess.CreateCards();
                if (null != cardDal)
                {
                    if (cardDal.cardNumberExists(cardnumber)) 
                    {
                        Model.Cards cardModel = cardDal.GetModelByCradNum(cardnumber);
                        if (cardDal.cardNumberValiddate(cardnumber) && (!(cardDal.cardNumberUsed(cardnumber))))
                        {
                            DAL.CardTypeDAL cardTypeDal = (DAL.CardTypeDAL)DALFAC.DataAccess.CreateCardType();
                            if(null != cardTypeDal)
                            {
                                //充值
                                Model.CardType cardTypeModel = cardTypeDal.GetModelByCardType(cardModel.getCD_cardType());
                                int value = cardTypeModel.getCT_cardValue() + account.getAC_balance();
                                account.setAC_balance(value);
                                validator = accountDal.Update(account);

                                //设置充值卡已使用
                                cardModel.setCD_used(true);
                                cardDal.Update(cardModel);
                            }
                        }
                    }
                }
            }
            return validator;
        }


        public bool consume(int accountId, int num)
        {
            bool validator = false;
            Model.Accounts account = new Model.Accounts();
            DAL.AccountsDAL accountDal = (DAL.AccountsDAL)DALFAC.DataAccess.CreateAccounts();

            if (null != accountDal)
            {
                if (accountDal.accountExists(accountId))
                {
                    account = accountDal.GetModelByUserId(accountId);
                    if (num > account.getAC_balance())
                    {
                        account.setAC_balance(account.getAC_balance() - num);
                        validator = accountDal.Update(account);
                    }
                }                                                  
            }
            return validator;
        }
    }
}
